const router = require("express")();
module.exports = router;

const db = require("../core/Data");
const User = require("../core/User");


const PAGE_CREATE = "/-/create.html";
const PAGE_LOGIN = "/-/login.html";
const PAGE_BAD = "/-/bad.html";

router.get("/", User.middle_checker(false), (req, res, next) => {

    if (req.user) res.redirect(PAGE_CREATE);
    else res.redirect(PAGE_LOGIN);
    return;

});

router.get("/:id", async (req, res) => {
    let id = (req.params || {}).id;
    if (!id) return res.redirect(`${PAGE_BAD}?t=NoID&d=${id}`);
    if (!APP.tools.radixInMax(id)) return res.redirect(`${PAGE_BAD}?t=NaN&d=${id}`);

    let url = await db.getUrl(id);
    if (url) res.redirect(url);
    return res.redirect(`${PAGE_BAD}?t=404%20|%20Not%20Found&d=${id}`);

    //TODO
});